package com.shanghaionstar.notifysetting.service;

import android.content.Context;

import com.google.gson.Gson;
import com.onstar.cn.api.UsersService;
import com.onstar.cn.auth.userProfile.Vehicle;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.notify.NotifyCodeReq;
import com.onstar.cn.notify.NotifyConfigRes;
import com.onstar.cn.notify.ValidateNotifyConfigRes;
import com.onstar.cn.smart.BaseResponse;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.LinkedHashMap;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Rengui Lu on 2017/3/21.
 */

public class NotifySettingService extends BaseService<Object> {

    private Context context;

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public Object jsonToBean(String json) {
        return null;
    }


    public NotifySettingService(Context context) {
        this.context = context;
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public String getNotifyConfigJson(String type) throws OnStarException {
        UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.getNotifyPreference(NetConnectUtil.getAuthRequestEntity(context), ApplicationUtil.getInstance().getSubscriberID(),
                ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin(), type);
        addAuthorization(context, requestEntity);
        requestEntity = HttpInjectUtils.injectHttp(NotifySettingService.this.getClass(), "getNotifyConfigJson", requestEntity, String.class);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    public Observable<NotifyConfigRes> getNotifyConfig(final String type) {
        return Observable.defer(new Func0<Observable<NotifyConfigRes>>() {
            @Override
            public Observable<NotifyConfigRes> call() {
                try {
                    return Observable.just(GsonHelper.fromJson(getNotifyConfigJson(type), NotifyConfigRes.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public String getNotifyCodeJson(NotifyCodeReq req) throws OnStarException {
        UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = service.getNotifyCode(NetConnectUtil.getAuthRequestEntity(context), ApplicationUtil.getInstance().getSubscriberID());
        addAuthorization(context, requestEntity);
        requestEntity.setBody(new Gson().toJson(req));
        requestEntity = HttpInjectUtils.injectHttp(NotifySettingService.this.getClass(), "getNotifyCodeJson", requestEntity, NotifyCodeReq.class);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    public Observable<BaseResponse> getNotifyCode(final NotifyCodeReq req) {
        return Observable.defer(new Func0<Observable<BaseResponse>>() {
            @Override
            public Observable<BaseResponse> call() {
                try {
                    return Observable.just(GsonHelper.fromJson(getNotifyCodeJson(req), BaseResponse.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public String validateNotifyCodeJson(NotifyCodeReq req) throws OnStarException {
        UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = service.validateNotifyCode(NetConnectUtil.getAuthRequestEntity(context), ApplicationUtil.getInstance().getSubscriberID());
        addAuthorization(context, requestEntity);
        requestEntity.setBody(new Gson().toJson(req));
        requestEntity = HttpInjectUtils.injectHttp(NotifySettingService.this.getClass(), "validateNotifyCodeJson", requestEntity, NotifyCodeReq.class);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    public Observable<BaseResponse> validateNotifyCode(final NotifyCodeReq req) {
        return Observable.defer(new Func0<Observable<BaseResponse>>() {
            @Override
            public Observable<BaseResponse> call() {
                try {
                    return Observable.just(GsonHelper.fromJson(validateNotifyCodeJson(req), BaseResponse.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public String setNotifyConfigJson(NotifyConfigRes req) throws OnStarException {
        UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.setNotifyPreference(NetConnectUtil.getAuthRequestEntity(context), ApplicationUtil.getInstance().getSubscriberID(),
                ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin());
        addAuthorization(context, requestEntity);
        requestEntity.setBody(new Gson().toJson(req));
        requestEntity = HttpInjectUtils.injectHttp(NotifySettingService.this.getClass(), "setNotifyConfigJson", requestEntity, NotifyConfigRes.class);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    public Observable<BaseResponse> setNotifyConfig(final NotifyConfigRes req) {
        return Observable.defer(new Func0<Observable<BaseResponse>>() {
            @Override
            public Observable<BaseResponse> call() {
                try {
                    return Observable.just((BaseResponse) GsonHelper.fromJson(setNotifyConfigJson(req), BaseResponse.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public String validateNotifyConfigJson() throws OnStarException {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        Vehicle vehicle = ApplicationUtil.getInstance().getCurrentVehicle();
        RequestEntity requestEntity = service.checkNotifyPreference(NetConnectUtil.getAuthRequestEntity(context), ApplicationUtil.getInstance().getIdpID(),
                ApplicationUtil.getInstance().getAccountId(), vehicle == null ? "" : vehicle.getVin());
        addAuthorization(context, requestEntity);
        requestEntity = HttpInjectUtils.injectHttp(NotifySettingService.this.getClass(), "validateNotifyConfigJson", requestEntity);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    public Observable<ValidateNotifyConfigRes> validateNotifyConfig() {
        return Observable.defer(new Func0<Observable<ValidateNotifyConfigRes>>() {
            @Override
            public Observable<ValidateNotifyConfigRes> call() {
                try {
                    return Observable.just(GsonHelper.fromJson(validateNotifyConfigJson(), ValidateNotifyConfigRes.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    private void addAuthorization(Context context, RequestEntity requestEntity) {
        LinkedHashMap<String, Object> coHashMap = new LinkedHashMap<String, Object>();
        coHashMap.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(coHashMap);
    }
}
